package org.itboys.mobile.controller.site;

import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayAdvertisementDto;
import org.itboys.mobile.entity.mongo.SundayNews;
import org.itboys.mobile.entity.mongo.SundayNewsCategory;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.service.mongo.site.JoinSiteCountService;
import org.itboys.mobile.service.mongo.site.SundaySiteAdvertiseMentService;
import org.itboys.mobile.service.mongo.site.SundaySiteNewsService;
import org.itboys.mobile.service.mongo.web.SundayNewsCategoryService;
import org.itboys.mobile.service.mysql.site.SundaySiteProductService;
import org.itboys.mobile.service.mysql.web.product.SundayProductCategoryService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/23
 * 描述：雄安加盟网_前端资讯
 */
@Controller
@RequestMapping("/sunday/site/news")
public class SundaySiteNewsController extends BaseController{
    @Autowired
    private SundaySiteNewsService newsService;
    @Autowired
    private SundayNewsCategoryService categoryService;
    @Autowired
    private SundayProductCategoryService productCategoryService;
    @Autowired
    private SundaySiteAdvertiseMentService advertiseMentService;
    @Autowired
    private JoinSiteCountService countService;
    @Autowired
    private SundaySiteProductService productService;
    @RequestMapping("/index")
    public String  index(HttpServletRequest request,
                         HttpServletResponse response,
                         @RequestParam(value="categoryId",required=true)Long categoryId,
                         @RequestParam(value="page",required=false)Integer page,
                         Model model){
        //加载一些广告
        countService.count(model);
        Map<String,Object> param=new HashMap<String ,Object>();
        //加载常用搜索关键词
        List<SundayAdvertisementDto> common_searchs =advertiseMentService.getAdvertisementByZone("common_search");
        model.addAttribute("common_searchs",common_searchs);
        //获取所有资讯分类
        param.clear();
        param.put("isDeleted",0);
        param.put("parentId",0);
        List<SundayNewsCategory> newsCategories=categoryService.list(param);
        model.addAttribute("newsCategories",newsCategories);
        //获取当前分类
        SundayNewsCategory thisCategory=categoryService.getById(categoryId);
        model.addAttribute("thisCategory",thisCategory);
        //获取当前分类资讯
        PageResult<SundayNews> result=newsService.select(request,categoryId,page);
        model.addAttribute("newss",result.getData());
        //获取推荐的前五个
        List<SundayNews> recommendNewss=newsService.getRecommendNews();
        model.addAttribute("recommendNewss",recommendNewss);
        //获取排名项目
        List<SundayProduct> paimingProducts=productService.selectPaiming(10,null);
        model.addAttribute("paimingProducts",paimingProducts);

        model.addAttribute("currentPage",page);
        model.addAttribute("totalPage",result.getTotal()/5+1);
        return "/join/site/news/index";
    }

    /**
     * 单个资讯页面
     * @param request
     * @param response
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/findOne")
    public String  findOne(HttpServletRequest request,
                         HttpServletResponse response,
                         @RequestParam(value="id",required=true)Long id,
                         Model model){
        //加载一些广告
        countService.count(model);
        Map<String,Object> param=new HashMap<String ,Object>();
        //加载常用搜索关键词
        List<SundayAdvertisementDto> common_searchs =advertiseMentService.getAdvertisementByZone("common_search");
        model.addAttribute("common_searchs",common_searchs);
        //获取所有资讯分类
        param.clear();
        param.put("isDeleted",0);
        param.put("parentId",0);
        List<SundayNewsCategory> newsCategories=categoryService.list(param);
        model.addAttribute("newsCategories",newsCategories);
        //获取当前资讯
        SundayNews news=newsService.getById(id);
        model.addAttribute("news",news);
        //获取当前分类
        SundayNewsCategory thisCategory=categoryService.getById(news.getCategoryId());
        model.addAttribute("thisCategory",thisCategory);
        //获取推荐的前五个
        List<SundayNews> recommendNewss=newsService.getRecommendNews();
        model.addAttribute("recommendNewss",recommendNewss);
        //获取相关资讯六个
        List<SundayNews> xiangguanNews=newsService.getXiangguangNews(id);
        model.addAttribute("xiangguanNews",xiangguanNews);
        //获取排名项目
        List<SundayProduct> paimingProducts=productService.selectPaiming(10,null);
        model.addAttribute("paimingProducts",paimingProducts);
        return "/join/site/news/findOne";
    }
}
